import React, {Component} from "react";


class DetailMenu extends Component {
    constructor(props) {
        super(props);
        this.state = {
            shoppingCartCount: props.shoppingCartCount
        }
    }

    componentWillUpdate(nextProps) {
        if (this.state.shoppingCartCount != nextProps.shoppingCartCount) {
            this.setState({
                shoppingCartCount: nextProps.shoppingCartCount
            });
        }
    }

    render() {
        const {item} = this.props;
        return (
            <div className="detail-menu">
                <div className="detail-menu-shopping-cart" onClick={this._goToShoppingCart.bind(this)}>
                    <img className="detail-menu-car"
                         src="http://oeb6w1s1c.bkt.clouddn.com/170510135332483252598645.png"></img>
                    {this.shoppingCartCount()}
                </div>

                <div className="detail-menu-buy" onClick={this._toBuy.bind(this, item)}>
                    <span>立即购买</span>
                </div>
                <div className="detail-menu-add-shopping-cart" onClick={this._addToCar.bind(this, item)}>
                    <span>加入购物车</span>
                </div>
            </div>
        )
    }

    _addToCar(item) {
        this.props.addToShoppingCar(item);
    }

    _toBuy(item) {
        this.props.toBuy(item);
    }

    shoppingCartCount() {
        console.log("_checkedAll" + this.state.shoppingCartCount);
        if (this.state.shoppingCartCount > 0) {
            return (
                <i className="goods-detail-shopping-cart-count">{this.state.shoppingCartCount}</i>
            )
        }
    }

    _goToShoppingCart() {
        window.location.href = "/#/mall/shoppingCart";
    }
}

export default DetailMenu